package com.shouer.doc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.shouer.doc.R;

/**
 * Created by ZXL on 2016/12/5.
 */

public class CommonDialog extends Dialog {

    private View mDialogView;
    private TextView messageView, leftBtn, rightBtn, centerBtn;
    private View twoLayout;
    private DialogDismissListener dismissListener;


    public CommonDialog(Context context) {
        super(context);
        init(context);
    }

    public CommonDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected CommonDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity= Gravity.CENTER_HORIZONTAL|Gravity.TOP;
        getWindow().setAttributes((WindowManager.LayoutParams) params);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_commom_layout, null);
        messageView = (TextView) mDialogView.findViewById(R.id.message);

        leftBtn = (TextView) mDialogView.findViewById(R.id.dialog_left_btn);
        rightBtn = (TextView) mDialogView.findViewById(R.id.dialog_right_btn);
        centerBtn = (TextView) mDialogView.findViewById(R.id.dialog_center_btn);
        twoLayout = mDialogView.findViewById(R.id.two_buttons);
        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
    }

    public void withBtnClickListener(View.OnClickListener clickListener) {
        leftBtn.setOnClickListener(clickListener);
        leftBtn.setTag(R.id.dialog_btn_tag, 0);
        rightBtn.setOnClickListener(clickListener);
        rightBtn.setTag(R.id.dialog_btn_tag, 0);
        centerBtn.setOnClickListener(clickListener);
        centerBtn.setTag(R.id.dialog_btn_tag, 0);
    }

    public void withBtnTag(int tag) {
        leftBtn.setTag(R.id.dialog_btn_tag, tag);
        rightBtn.setTag(R.id.dialog_btn_tag, tag);
        centerBtn.setTag(R.id.dialog_btn_tag, tag);
    }

    public void withBtnsText(String leftBtnTxt, String rightBtnTxt) {
        centerBtn.setVisibility(View.GONE);
        leftBtn.setVisibility(View.VISIBLE);
        rightBtn.setVisibility(View.VISIBLE);
        twoLayout.setVisibility(View.VISIBLE);
        leftBtn.setText(leftBtnTxt);
        rightBtn.setText(rightBtnTxt);
    }

    public void withCenterBtnTxt(String centerBtnTxt) {
        twoLayout.setVisibility(View.GONE);
        centerBtn.setVisibility(View.VISIBLE);
        centerBtn.setText(centerBtnTxt);
    }

    public void withMessage(String message) {
        messageView.setText(message);
    }


    public void withDialogDismissListener(DialogDismissListener dismissListener) {
        this.dismissListener = dismissListener;
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
        if (null!=dismissListener)
        {
            dismissListener.onDialogDismiss();
        }
    }
}
